/*
 * $Id: Level1PlanningDao.java 1759 2011-08-31 07:43:16Z abie $
 * 
 * Copyright (c) 2001-2008 Accentrix Company Limited. All Rights Reserved.
 * 
 * Accentrix Company Limited. ("Accentrix") retains copyright on all text, source
 * and binary code contained in this software and documentation. Accentrix grants
 * Licensee a limited license to use this software, provided that this copyright
 * notice and license appear on all copies of the software. The software source
 * code is provided for reference purposes only and may not be copied, modified 
 * or distributed.
 * 
 * THIS SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS," WITHOUT ANY WARRANTY OF
 * ANY KIND UNLESS A SEPARATE WARRANTIES IS PURCHASED FROM ACCENTRIX AND REMAINS
 * VALID.  ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 * PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. ACCENTRIX SHALL NOT BE LIABLE
 * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING OR MODIFYING THE
 * SOFTWARE OR ITS DERIVATIVES.
 * 
 * IN NO EVENT WILL ACCENTRIX BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
 * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES,
 * HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE
 * USE OF OR INABILITY TO USE SOFTWARE, EVEN IF ACCENTRIX HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 */
package com.lightsaber.trade.cloth.dao.planning;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;
import org.springside.modules.orm.Page;

import com.lightsaber.trade.core.dao.BaseDao;
import com.lightsaber.trade.cloth.entity.category.Category;
import com.lightsaber.trade.cloth.entity.category.CategoryNode;
import com.lightsaber.trade.cloth.entity.planning.Level1Planning;

@Component
public class Level1PlanningDao extends BaseDao<Level1Planning, String> {

    public void findPage(final Page<Level1Planning> page, final String[] categoryIds) {

        if (categoryIds.length == 0) {
            return;
        }
//        String ids = "";
//        for (int i = 0; i < categoryIds.length; i++) {
//            if (i == 0) {
//                ids = "" + categoryIds[i] + "";
//            }else{
//                ids += "," + categoryIds[i] + "";
//            }
//        }
        String hql = "select lp from Level1Planning lp where lp.category.id in(:ids) "
                + " order by lp.planningExercise,lp.category.planningExerciseName, lp.category.branchName, lp.name asc";
        
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("ids", categoryIds);

        findPage(page, hql, map);
        
//        if (categoryIds.length > 1) {
//            in("category.id", categoryIds);
//        } else if (categoryIds.length == 1) {
//            eq("category.id", categoryIds[0]);
//        } else if (categoryIds.length == 0) {
//            return;
//        }
//        addOrder(Order.asc("planningExercise"));
//        addOrder(Order.asc("name"));
//
//        findPage(page);
       
    }

    public Level1Planning findEndorsedByMasterTree(final Category masterTree) {
        eq("category", masterTree);
        return unique();
    }

    public long countNameByCategoryId(final String id, final String name,
            final CategoryNode owerNode) {

        ne("id", id);
        eq("name", name);

        createAlias("category", "c");
        eq("c.ownerNode", owerNode);

        return count();
    }

    public Level1Planning findByBranchTree(final Category branchTree) {
        eq("category", branchTree);

        List<Level1Planning> level1Plannings = list();

        if (level1Plannings.size() > 0) {
            return level1Plannings.get(0);
        }

        return null;
    }

    public Level1Planning findByBranchTree(final String branchTreeId) {
        eq("category.id", branchTreeId);

        List<Level1Planning> level1Plannings = list();

        if (level1Plannings.size() > 0) {
            return level1Plannings.get(0);
        }

        return null;
    }

    public Level1Planning findByNameAndOwerNode(final String name, final CategoryNode ownerNode) {
        eq("name", name);

        createAlias("category", "c");
        eq("c.ownerNode", ownerNode);

        return unique();
    }

}
